﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Objects;
using FileDB;

namespace Rem.Objects.Forms
{
    public partial class UcEmployee : BaseUserControl
    {
        public UcEmployee()
        {
            InitializeComponent();
            if (this.Data == null)
            {
                this.Data = new Employee() { Id = QueryConstants.UnsavedObjectId, BasicSalary = 0.00 };
            }
            BindingData();
        }
        private Employee Employee { get { return this.Data as Employee; } }

        public override Object Data
        {
            get
            {
                return base.Data;
            }
            set
            {
                //todo: add event instead of the code
                Employee info = value as Employee;
                if (info == null)
                {
                    throw new Exception("object can't set to null");
                }
                base.Data = value;
                employeeBindingSource.DataSource = this.Data;
            }
        }
        void BindingData()
        {

            employeeBindingSource.DataSource = this.Data;
            employeeBindingSource.DataSourceChanged += (sender, arg) =>
            {
                DisplayAddtionalInfo();
            };
        }

        private void DisplayAddtionalInfo()
        {
            lblID.Text = string.Format(QueryConstants.IDTemplate, Employee.Id.ToString());
            //this.basicSalaryTextBox.Text = Employee.BasicSalary.ToString();
            this.CopyAsURLToolStripMenuItem.Visible = false;
            this.SendViaMailToolStripMenuItem.Visible = false;
            this.CopyIDToolStripMenuItem.Visible = false;
        }

        private void DoToolBarActions(ToolBarActions action)
        {
            switch (action)
            {
                case ToolBarActions.New:
                    (new EmployeeForm()).Show();
                    break;
                case ToolBarActions.Open:
                    var Ids = GoToForm.GetID("Id", "Name", BaseObjectTypes.Employee);
                    int objectId = Ids.Count == 1 ? Ids[0] : QueryConstants.UnsavedObjectId;
                    if (objectId > 0)
                    {
                        Employee employee = (from e in RemDatabaseEntities.Employees
                                             where e.Id == objectId
                                             select e
                                             ).SingleOrDefault();
                        if (employee != null && employee.Id > 0)
                        {
                            EmployeeForm f = new EmployeeForm(employee);
                            f.Show();
                            f.Dock = DockStyle.Fill;
                        }
                    }
                    break;
                case ToolBarActions.Clone:
                    if (this.Employee.Id > 0)
                    {
                        EmployeeForm form = new EmployeeForm((this.Employee.Clone()) as Employee);
                        form.Show();
                    }
                    break;
                case ToolBarActions.Save:
                    Save();
                    break;
                case ToolBarActions.Delete:
                    if (this.Employee.Id <= 0)
                    {
                        return;
                    }
                    DialogResult result = MessageBox.Show("确定删除此员工！", "员工删除", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        try
                        {
                            if (this.Employee.Delete())
                            {
                                if (base.Deleted != null)
                                {
                                    base.Deleted(this, null);
                                }
                            }
                            else
                            {
                                MessageBox.Show("删除出错或数据不存在！", "员工删除", MessageBoxButtons.OK);
                            }
                        }
                        catch (InvalidOperationException invalidException)
                        {
                            MessageBox.Show("删除出错！", "客户删除", MessageBoxButtons.OK);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("已有数据关联到此员工！", "员工删除", MessageBoxButtons.OK);
                        }
                    }

                    break;
                case ToolBarActions.Refresh:
                    if (this.Employee != null && this.Employee.Id > 0)
                    {
                    }
                    //todo:
                    break;
                default:
                    break;
            }
        }

        public override bool Save()
        {
            //double salary;
            //if (!double.TryParse(this.basicSalaryTextBox.Text.Trim(), out salary))
            //{
            //    MessageBox.Show("必须为数据！","提示",MessageBoxButtons.OK);
            //    return false;
            //}
            AssignEmployee();
            this.Employee.Save();
            DisplayAddtionalInfo();
            return base.Save();
        }

        private void AssignEmployee()
        {
            this.Employee.Name = this.nameTextBox.Text;
            this.Employee.Title = this.titleTextBox.Text;
            //this.Employee.BasicSalary = double.Parse(this.basicSalaryTextBox.Text.Trim());
            this.Employee.Birthday = this.birthdayDateTimePicker.Value;
            this.Employee.Phones = this.phonesTextBox.Text;
            this.Employee.Memo = this.memoTextBox.Text;
        }

        #region toolbar actions
        private void newToolStripButton_Click(object sender, EventArgs e)
        {
            this.DoToolBarActions(ToolBarActions.New);
        }

        private void openToolStripButton_Click(object sender, EventArgs e)
        {
            this.DoToolBarActions(ToolBarActions.Open);
        }

        private void cloneToolStripButton_Click(object sender, EventArgs e)
        {
            this.DoToolBarActions(ToolBarActions.Clone);
        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            this.DoToolBarActions(ToolBarActions.Save);
        }

        private void deleteToolStripButton_Click(object sender, EventArgs e)
        {
            this.DoToolBarActions(ToolBarActions.Delete);
        }

        private void worklogsToolStripButton_Click(object sender, EventArgs e)
        {
            this.DoToolBarActions(ToolBarActions.Worklog);
        }

        private void DependenciesToolStripButton1_Click(object sender, EventArgs e)
        {
            this.DoToolBarActions(ToolBarActions.Delete);
        }

        private void helpToolStripButton_Click(object sender, EventArgs e)
        {
            this.DoToolBarActions(ToolBarActions.Help);
        }

        private void CopyAsURLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.DoToolBarActions(ToolBarActions.CopyAsURL);
        }

        private void SendViaMailToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.DoToolBarActions(ToolBarActions.SendViaMail);
        }

        private void CopyIDToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.DoToolBarActions(ToolBarActions.CopyID);
        }
        #endregion

        private void birthdayDateTimePicker_ValueChanged(object sender, EventArgs e)
        {

        }

        private void basicSalaryTextBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void UcEmployee_Load(object sender, EventArgs e)
        {

        }


    }
}
